package leetcode.D100.T43;

/**
 * @File Info: leetcode -- <Solution>
 * @Author: 18362
 * @Create: 2022-03-11 22:45:21 星期五
 */

class Solution {
    public String multiply(String num1, String num2) {
        if (num1.charAt(0) == '0' || num2.charAt(0) == '0')
            return "0";
        int[] temp = new int[num1.length() + num2.length()];
        for (int i = 0; i < num1.length(); i++) {
            for (int j = 0; j < num2.length(); j++) {
                int a = num1.charAt(num1.length()-1-i) - '0';
                int b = num2.charAt(num2.length()-1-j) - '0';
                int prod = a * b + temp[i+j];
                temp[i+j+1] += prod / 10;
                temp[i+j] = prod % 10;
            }
        }
        StringBuilder sb = new StringBuilder();
        int j = num1.length() + num2.length() - 1;
        if (temp[num1.length()+num2.length()-1] == 0)
            j--;
        while (j >= 0) {
            sb.append(temp[j--]);
        }
        return sb.toString();
    }
}